/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package certificate.controller;

import certificate.database.ConnectionDB;
import certificate.entity.Course;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author minhtuan
 */
public class CourseController implements BaseController<Course> {

    Connection conn = null;
    Statement stm = null;
    PreparedStatement pstm = null;

    @Override
    public boolean addNew(Course temp) throws Exception {
        if (!getStatusById(temp.getIdCourse())) {
            conn = ConnectionDB.getConnection();
            pstm = conn.prepareStatement("INSERT INTO Course VALUES (?, ?, ?, ?)");
            pstm.setString(1, temp.getIdCourse());
            pstm.setString(2, temp.getName());
            pstm.setFloat(3, temp.getPrice());
            pstm.setString(4, temp.getTimeAlong());
            int executeUpdate = pstm.executeUpdate();
            if (executeUpdate != 0) {
                conn.close();
                return true;
            }
            conn.close();
            return false;
        }
        return false;
    }

    @Override
    public boolean deleteById(String id) throws Exception {
        if (getStatusById(id)) {
            conn = ConnectionDB.getConnection();
            pstm = conn.prepareStatement("DELETE FROM Course WHERE IdCourse = ?");
            pstm.setString(1, id);
            int executeUpdate = pstm.executeUpdate();
            if (executeUpdate != 0) {
                conn.close();
                return true;
            }
            conn.close();
            return false;
        }
        return false;
    }

    @Override
    public boolean update(Course temp) throws Exception {
        conn = ConnectionDB.getConnection();
        pstm = conn.prepareStatement("UPDATE Course SET name = ?, price = ?, timeAlong = ? WHERE idCourse = ?");
        pstm.setString(1, temp.getName());
        pstm.setFloat(2, temp.getPrice());
        pstm.setString(3, temp.getTimeAlong());
        pstm.setString(4, temp.getIdCourse());
        int executeUpdate = pstm.executeUpdate();
        if (executeUpdate != 0) {
            conn.close();
            return true;
        }
        conn.close();
        return false;
    }

    @Override
    public List<Course> getAll() throws Exception {
        List<Course> list = new ArrayList<Course>();
        conn = ConnectionDB.getConnection();
        stm = conn.createStatement();
        ResultSet rs = stm.executeQuery("SELECT * FROM Course");
        while (rs.next()) {
            Course c = new Course();
            c.setIdCourse(rs.getString("idCourse"));
            c.setName(rs.getString("Name"));
            c.setPrice(rs.getFloat("Price"));
            c.setTimeAlong(rs.getString("TimeAlong"));
            list.add(c);
        }
        conn.close();
        return list;
    }

    @Override
    public boolean getStatusById(String id) throws Exception {
        conn = ConnectionDB.getConnection();
        pstm = conn.prepareStatement("SELECT * FROM Course WHERE idCourse = ?");
        pstm.setString(1, id);
        ResultSet rs = pstm.executeQuery();
        if (rs.next()) {
            conn.close();
            return true;
        }
        conn.close();
        return false;
    }
}
